package com.school.controller;

import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;

import javax.swing.JPanel;

import com.school.model.dao.FachHome;
import com.school.model.dao.KlasseHome;
import com.school.model.dao.SchuelerHome;
import com.school.model.dao.SchuleHome;
import com.school.model.entity.Fach;
import com.school.model.entity.Klasse;
import com.school.model.entity.Schueler;
import com.school.model.entity.Schule;
import com.school.view.beans.ViewTableBean;
import com.school.view.model.StudentTableModel;

public class ButtonPanel extends JPanel implements ActionListener {

	/**
	 * 
	 */
	private static final long serialVersionUID = -2808255229906361350L;
	private StudentTableModel model;
	private SchuleHome schuleHome;
	private KlasseHome klasseHome;
	private SchuelerHome schuelerHome;
	private FachHome fachHome;

	private Schule schule;
	private Klasse klasse;
	private Schueler schueler;
	private Fach fach;

	public ButtonPanel(StudentTableModel model) {
		this.model = model;

	}

	@Override
	public void actionPerformed(ActionEvent arg0) {
		// Added gui bean to table
		ViewTableBean tableBean = new ViewTableBean("Aleks", "Tolev", 2);
		model.addViewTableBean(tableBean);

		// Now add the information to database
		initalizeDAOAndModel();

		fillSchule("Test");
		fillKlasse("8 A");
		fillSchueler("Aleks Tolev");
		fillFach(2, "Englisch");
		schuleHome.persist(schule);
		klasseHome.persist(klasse);
		schuelerHome.persist(schueler);
		fachHome.persist(fach);
	}

	private void initalizeDAOAndModel() {
		schuleHome = new SchuleHome();
		klasseHome = new KlasseHome();
		schuelerHome = new SchuelerHome();
		fachHome = new FachHome();

		schule = new Schule();
		klasse = new Klasse();
		schueler = new Schueler();
		fach = new Fach();
	}

	private void fillSchule(String name) {
		schule.setName(name);
	}

	private void fillKlasse(String name) {
		klasse.setSchule(schule);
		klasse.setName(name);
		schule.getKlasses().add(klasse);
	}

	private void fillSchueler(String name) {
		schueler.setKlasse(klasse);
		schueler.setName(name);
		fach.getSchuelers().add(schueler);
	}

	private void fillFach(int note, String name) {
		fach.setName(name);
		fach.setNote(note);
	}

}
